/**
 * 
 */
package db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;

import db.DBHelper;

/**
 * @author Andrew
 * 
 */
public class Registration {

	// set connection to DBHelper
	DBHelper helper = new DBHelper();
	
	private PreparedStatement validateUserDoesNotExistStatement;
	private PreparedStatement registerUserStatement;

	public Registration() {
	}

	/**
	 * register a new user
	 * 
	 * @param userName
	 *            user name
	 * @param userPhoneNumber
	 *            user phone number
	 * @param userEmailAddress
	 *            user email
	 * @param userPassword
	 *            user password
	 * @param userBirthDay
	 *            user birthday
	 */
	public int registerUsers(String userName, String userPhoneNumber,
			String userEmailAddress, String userPassword, String userBirthDay,
			int role) {
		// role 3=mgmt 2=server 1=customer
		int userExist2 = 0;
		ResultSet rs = null;
		Connection conn = helper.connection();

		java.util.Date date = new java.util.Date();
		Timestamp currentTimestamp = new Timestamp(date.getTime());

		String cPhoneNumber = "";

		// validate there are ten digits in the cleaned phone number
		if (db.FormatAndValidation.validPhoneNumber(userPhoneNumber)) {

			try {

				// clean phone number - remove everything but the numbers
				cPhoneNumber = db.FormatAndValidation
						.cleanPhoneNumber(userPhoneNumber);

				validateUserDoesNotExistStatement = conn
						.prepareStatement(DBHelper.validateUserDoesNotExistStatement);

				validateUserDoesNotExistStatement.setString(1, cPhoneNumber);

				System.out.println(userName);
				System.out.println(cPhoneNumber);
				System.out.println(userEmailAddress);
				System.out.println(userPassword);
				System.out.println("larry test");
				System.out.println(userBirthDay);
				System.out.println(role);
				System.out.println(currentTimestamp);

				rs = validateUserDoesNotExistStatement.executeQuery();
				while (rs.next()) {
					userExist2 = rs.getInt("UserExist");// see is userExist
														// already

				}
				System.out.println(userExist2);

				rs.close();

				if (userExist2 == 0) {

					registerUserStatement = conn
							.prepareStatement(DBHelper.registerUserStatement);

					registerUserStatement.setString(1, userName);
					registerUserStatement.setString(2, cPhoneNumber);
					registerUserStatement.setString(3, userEmailAddress);
					registerUserStatement.setString(4, userPassword);
					registerUserStatement.setString(5, userBirthDay);
					registerUserStatement.setInt(6, role);
					registerUserStatement.setTimestamp(7, currentTimestamp);
					registerUserStatement.setTimestamp(8, currentTimestamp);
					registerUserStatement.executeUpdate();
					return userExist2;
				} else {
					return userExist2;

				}

			}

			catch (Exception e) {

				System.out.println("Error in registerUsers: "
						+ e.getClass().getName() + ":" + e.getMessage());
				userExist2 = 9;
			} finally {

				DBHelper.disconnect(rs, validateUserDoesNotExistStatement, conn);
				DBHelper.disconnect(rs, registerUserStatement, conn);
				
			}
		} else {
			// phone number did not pass validation, return error code for error
			userExist2 = 9;
		}

		return userExist2;
	}

}
